package com.fpost.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fpost.dao.GenericDAO;
import com.fpost.domain.ArticleVersion;

public class ArticleVersionDAO extends GenericDAO<ArticleVersion> {
		
	@Override
	public Long save(ArticleVersion articleVersion) throws HibernateException {
		Long articleVersionId = null;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			articleVersionId = (Long) session.save(articleVersion);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			throw e;
		} finally {
			session.close();
		}
		return articleVersionId;
	}

	@Override
	public void delete(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(get(id));
			session.getTransaction().commit();
		} catch (HibernateException e) {
			throw e;
		} finally {
			session.close();
		}
	}

	@Override
	public void delete(ArticleVersion articleVersion) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(articleVersion);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			throw e;
		} finally {
			session.close();
		}
	}

	@Override
	public ArticleVersion get(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		ArticleVersion articleVersion = null;
		try {
			session.beginTransaction();
			articleVersion = (ArticleVersion) session.get(ArticleVersion.class, id);
			//session.update(obj);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return articleVersion;
	}

	@SuppressWarnings("unchecked")
	public ArticleVersion getByArticleVersionId(long articleVersionId) throws HibernateException {
		Session session = getSessionFactory().openSession();
		ArticleVersion articleVersion = (ArticleVersion)session.createCriteria(ArticleVersion.class)
				.add(Restrictions.idEq(new Long(articleVersionId))).uniqueResult();
		return articleVersion;
	}
	
	@SuppressWarnings("unchecked")
	public List<ArticleVersion> getByInputKey(String input) throws HibernateException {
		Session session = getSessionFactory().openSession();
		List<ArticleVersion> ArticleVersions = (List<ArticleVersion>) session.createCriteria(ArticleVersion.class)
				.add(Restrictions.like("body", "%" + input + "%"))
				// .add( Restrictions.like("keywords", "%" + input + "%"))
				.list();		
		return ArticleVersions;
	}
	
	@SuppressWarnings("unchecked")
	public List<ArticleVersion> getByInputAndType(String input, long articleTypeId) throws HibernateException {
		Session session = getSessionFactory().openSession();
		List<ArticleVersion> ArticleVersions = (List<ArticleVersion>) session.createCriteria(ArticleVersion.class)
				.add(Restrictions.like("body", "%" + input + "%"))
				.add(Restrictions.eq("articleTypeId", new Long(articleTypeId)))
				.list();
		return ArticleVersions;
	}
	
	@Deprecated
	@SuppressWarnings("unchecked")
	public List<ArticleVersion> getByInputAndTypeHQL(String input, long articleTypeId) throws HibernateException {
		
		String queryStr = "from ArticleVersion where body like '%:input%' and articleTypeId =:articleTypeId";
		List<ArticleVersion> ArticleVersions = null;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			Query query = session.createQuery(queryStr);
			query.setParameter("input", input);
			query.setParameter("articleTypeId", articleTypeId);
			ArticleVersions = (List<ArticleVersion>)query.list();
			session.getTransaction().commit();			
		} catch (HibernateException e) {
			throw e;
		} finally {
			session.close();
		}
		return ArticleVersions;
	}

	@Deprecated
	public void saveOrUpdate(ArticleVersion articleVersion) throws HibernateException {

	}
	
	public List<ArticleVersion> getAll() {
		Session session = getSessionFactory().openSession();
		List<ArticleVersion> users = 
				(List<ArticleVersion>) session.createCriteria(ArticleVersion.class)
				//.addOrder( Order.desc("createdOn"))				
				//.setMaxResults(50)
				.list();
		return users;		
	}

}
